<?php

namespace app\admin\model;
use think\Db;
use think\Model;

class OrderModel extends Model
{
    public function showorderlist()
    {
        $data = Db::table('order')
                        ->field('id, price, count, userid')
                        ->select();
       // var_dump($data);
        for($a=0; $a<count($data); $a++){
            $username = Db::table('user') ->field('nickname') ->where('id',$data[$a]['userid']) ->select();
           // var_dump($username);die;
            $data[$a]['username'] = $username[0]['nickname'];
        }
//        echo 111;die;
//        var_dump($data);die;
        return $data;
    }

    public function showdetails($id)
    {
//        var_dump($id);die;
        $data = Db::table('order')
                        ->field('userid, price')
                        ->where('id', $id)
                        ->find();
       // var_dump($data);die;

        $usermess = Db::table('user')
                        ->field('nickname, tel')
                        ->where('id', $data['userid'])
                        ->find();

//        var_dump($usermess);die;

        $goodsmess = Db::table('order_goods')
                        ->field('count, price, gid')
                        ->where('oid',$id)
                        ->select();
       // var_dump(count($goodsmess));die;

        for($g=0; $g<count($goodsmess); $g++){
            $goodsdetails = Db::table('goods')
                        ->field('pictrue, name, price')
                        ->where('id',$goodsmess[$g]['gid'])
                        ->select();

            $goodsdetails[0]['userid'] = $data['userid'];
            $goodsdetails[0]['total'] = $data['price'];
            $goodsdetails[0]['username'] = $usermess['nickname'];
            $goodsdetails[0]['tel'] = $usermess['tel'];
            $goodsdetails[0]['count'] = $goodsmess[$g]['count'];
            $goodsdetails[0]['oneprice'] = $goodsmess[$g]['price'];
            $goodsdetails[0]['gid'] = $goodsmess[$g]['gid'];

        }
        
       // var_dump($goodsdetails);die;
        return $goodsdetails;
    }

    public function showcontlist()
    {
        $data = Db::table('order')
            ->field('id, price, count, userid, status')
            ->select();
//        var_dump($data);die;

        for($a=0; $a<count($data); $a++){
            $username = Db::table('user') ->field('nickname') ->where('id',$data[$a]['userid']) ->select();
            $data[$a]['username'] = $username[0]['nickname'];
        }

//        var_dump($data);die;
        return $data;
    }

    public function conrl($id)
    {
        $status = ['status'=> '2'];
        $data = Db::table('order')
            ->where('id',$id)
            ->update($status);

        return $data;
    }

}
















